package org.itboys.core.service.mysql.web;

import org.itboys.commons.ErrorException;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.core.common.ExceptionEnum;
import org.itboys.core.common.SundayCommonConstants;
import org.itboys.core.common.WalletRecordEnum;
import org.itboys.core.dao.wallet.SundayWalletMapper;
import org.itboys.core.dao.wallet.SundayWalletRecordMapper;
import org.itboys.core.entity.mysql.wallet.SundayWallet;
import org.itboys.core.entity.mysql.wallet.SundayWalletExample;
import org.itboys.core.entity.mysql.wallet.record.SundayWalletRecord;
import org.itboys.core.entity.mysql.wallet.record.SundayWalletRecordExample;
import org.itboys.core.util.MybatisParamUtil;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Author：贾琦磊
 * @Date：2019/1/7
 * @Description：供应商收入明细
 */
@Service
public class SundayCompanyWalletService{

    @Autowired
    private SundayWalletMapper walletMapper;
    @Autowired
    private SundayWalletRecordMapper recordMapper;

    public PageResult<SundayWalletRecord> select(MybatisParamUtil paramUtil, Long memberId,Date createTime_start,Date createTime_end){
        SundayWalletExample example=new SundayWalletExample();
        example.createCriteria().andIsDeletedEqualTo(SundayCommonConstants.NO)
                .andMemberIdEqualTo(memberId);
        List<SundayWallet> wallets = walletMapper.selectByExample(example);
        if(ListUtil.isNull(wallets))throw new ErrorException(ExceptionEnum.GET_WALLET_EXCEPTION.getValue());
        SundayWallet wallet=wallets.get(0);
        Integer[] types={WalletRecordEnum.CHIYOUSHANG.getCode(),
                WalletRecordEnum.PEISONGSHANG.getCode(),
                WalletRecordEnum.HUISHOUSHANG.getCode()};
        SundayWalletRecordExample _example=new SundayWalletRecordExample();
        SundayWalletRecordExample.Criteria criteria = _example.createCriteria().andIsDeletedEqualTo(SundayCommonConstants.NO)
                .andWalletIdEqualTo(wallet.getId()).andTypeIn(Arrays.asList(types));
        if(createTime_start!=null)criteria.andCtGreaterThanOrEqualTo(createTime_start);
        if(createTime_end!=null) criteria.andCtLessThanOrEqualTo(createTime_start);
        long total = recordMapper.countByExample(_example);
        _example.setStartRow(paramUtil.getRowStart());
        _example.setPageSize(paramUtil.getRowSize());
        _example.setSortOrder(paramUtil.getSortOrder());
        _example.setOrderByClause(paramUtil.getSortName());
        List<SundayWalletRecord> sundayWalletRecords = recordMapper.selectByExample(_example);
        return new PageResult<>(sundayWalletRecords,total);
    }
}
